package test
{
	import _Pan3D.core.MathCore;
	import _Pan3D.display3D.Display3DContainer;
	import _Pan3D.display3D.Display3DMovie;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.program.Program3DManager;
	import _Pan3D.program.shaders.BaseShader;
	import _Pan3D.program.shaders.Md5Shader;
	import _Pan3D.program.shaders.StatShader;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import test.vo.Lizhilizhi;
	import test.vo.PersonSprite;
	import test.vo.StatVO;
	import test.vo.TreeDisplay3DSprite;
	import test.vo.TreeShader;
	import test.zhuwei.BuildDisplay3DContainer;
	import test.zhuwei.BuildDisply;
	import test.zhuwei.GroundDisply;
	import test.zhuwei.LizhiDisplay;
	import test.zhuwei.TiexiaoDisply;

	[SWF(backgroundColor="#FFFFFF",width="1024", height="600", frameRate="60")]
	public class testIndex extends BaseDemo
	{
		private var _programMd5:Program3D;
		private var _groundMode:Display3DContainer;
		private var _build3DContainer:BuildDisplay3DContainer;
		private var _tiexiaoContainer:Display3DContainer;
		private var _groundDispy:GroundDisply;
		private var _buildDispy:BuildDisply;
		private var _tiexiaoDispy:TiexiaoDisply;
		private var _bitmap:Bitmap;
		private var _lizhiDisplay:LizhiDisplay;

		
		public function testIndex()
		{
			super();
		}
		override protected function makeScene():void
		{

			var context3D : Context3D = Scene_data.context3D;
			TextureManager.getInstance().init(context3D);

			
			addGrounds();
			addBuilds();
			addTieXiaos();
			addLiZhis();
	
			
			this.addEventListener(Event.ENTER_FRAME, enterFrame);
			Scene_data.cam3D.distance = 1000;
			Scene_data.focus3D.angle_y = 0;
			Scene_data.focus3D.angle_x =-30;
			addView();
			
			

			

			
		}
		
		private function addLiZhis():void
		{
			var context3D : Context3D = Scene_data.context3D;
			_lizhiDisplay=new LizhiDisplay(context3D)
			
		}
		
		private function addView():void
		{
			var baseAn:Sprite=new Sprite;
			baseAn.graphics.beginFill(223344,1)
			baseAn.graphics.drawRect(0,0,100,30)
			baseAn.graphics.endFill();
			addChild(baseAn);
			baseAn.addEventListener(MouseEvent.CLICK,anMouseClik)
			function anMouseClik(evt:MouseEvent):void{
				Scene_data.ready=!Scene_data.ready;
				var context3D : Context3D = Scene_data.context3D;
				_build3DContainer.scaningShadow(context3D);
				var shadowBitmapData:BitmapData=_build3DContainer.scaningShadow(context3D);
				_bitmap.bitmapData=shadowBitmapData;
				Scene_data.texShadowMap.uploadFromBitmapData(shadowBitmapData);
				context3D.configureBackBuffer(1024, 600, 1, true);
				context3D.setCulling(Context3DTriangleFace.NONE);
				Scene_data.ready=!Scene_data.ready;
			}
			
			_bitmap=new Bitmap;
			_bitmap.y=50;
			_bitmap.scaleX=_bitmap.scaleY=0.2;
			//addChild(_bitmap);
			
		}
		
		private function addTieXiaos():void
		{
			var context3D : Context3D = Scene_data.context3D;
			_tiexiaoContainer = new Display3DContainer();
			_tiexiaoDispy=new TiexiaoDisply(context3D,_tiexiaoContainer);
		}
		
		private function addBuilds():void
		{
			
			var context3D : Context3D = Scene_data.context3D;
			_build3DContainer = new BuildDisplay3DContainer();
			_buildDispy=new BuildDisply(context3D,_build3DContainer);
		}
		private function addGrounds():void
		{
			
			var context3D : Context3D = Scene_data.context3D;
			_groundMode = new Display3DContainer();
			_groundDispy=new GroundDisply(context3D,_groundMode);
		}

		override protected function  enterFrame(event : Event) : void {
			
			
			keyControl.upData();
			MathCore._catch_cam(Scene_data.cam3D, Scene_data.focus3D);
			if (!Scene_data.ready ) return;
			var context : Context3D = Scene_data.context3D;
			context.clear(0, 0, 0, 1);
			projMatrix = new PerspectiveMatrix3D();
			projMatrix.perspectiveFieldOfViewRH(1, 1, 0.1, 9000);
			context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, projMatrix, true);
			context.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ZERO);
			context.setDepthTest(true, Context3DCompareMode.LESS);
			_build3DContainer.update();
			_groundMode.update();
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			_tiexiaoContainer.update();
			
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			context.setDepthTest(false, Context3DCompareMode.LESS);
			_lizhiDisplay.updata();
			context.present();

			
		}
	}
}